<?php

namespace app\admin\model;
use think\Model;
use think\Db;

class LeaseModel extends Model
{
    protected $name = 'lease';
    
    // 开启自动写入时间戳
    protected $autoWriteTimestamp = true;


    public function getByCount($map)
	{
		return Db::name('lease')->where($map)->count();
	}
    /**
     * [getAllLease 获取全部供租]
     * @author 【朱沪宁】
     */
    public function getAllLease()
    {
        return $this->order('id asc')->select();       
    }

 /**
     * 根据搜索条件获取用户列表信息
     * @author 【朱沪宁】
     */
    public function getLeaseByWhere($map, $Nowpage, $limits)
    {
        return  $this->field('think_lease.*,think_equipment.title as equipmentname,think_equipment.ggxh,think_equipment.sum,think_equipment.position,think_equipment.type,think_organization.title as orgname')
        ->join('think_equipment', 'think_equipment.id = think_lease.equipment_id')
        ->join('think_organization', 'think_organization.id = think_equipment.organization_id')
        ->where($map)->page($Nowpage, $limits)->order('id desc')->select();
        //echo Db::name("think_lease")->getLastSql();
       // return null;
    }
    public function getLeaseByWhereCount($map)
    {
        return  $this->field('think_lease.*,think_equipment.title as equipmentname,think_equipment.ggxh,think_equipment.sum,think_equipment.position,think_equipment.type,think_organization.title as orgname')
        ->join('think_equipment', 'think_equipment.id = think_lease.equipment_id')
        ->join('think_organization', 'think_organization.id = think_equipment.organization_id')
        ->where($map)->count();
        //echo Db::name("think_lease")->getLastSql();
       // return null;
    }
    /**
     * [insertLease 添加供租]
     * @author 【朱沪宁】
     */
    public function insertLease($param)
    {
        try{
            $result = $this->save($param);
            if(false === $result){     
                return ['code' => -1, 'data' => '', 'msg' => $this->getError()];
            }else{
                return ['code' => 1, 'data' => '', 'msg' => '供租添加成功'];
            }
        }catch( PDOException $e){
            return ['code' => -2, 'data' => '', 'msg' => $e->getMessage()];
        }
    }



    /**
     * [editMenu 编辑供租]
     * @author 【朱沪宁】
     */
    public function editLease($param)
    {
        try{
            $result = $this->save($param, ['id' => $param['id']]);
            if(false === $result){          
                return ['code' => 0, 'data' => '', 'msg' => $this->getError()];
            }else{
                return ['code' => 1, 'data' => '', 'msg' => '供租编辑成功'];
            }
        }catch( PDOException $e){
            return ['code' => 0, 'data' => '', 'msg' => $e->getMessage()];
        }
    }



    /**
     * [getOneMenu 根据供租id获取一条信息]
     * @return [type] [description]
     * @author 【朱沪宁】
     */
    public function getOneLease($id)
    {
        return $this->where('id', $id)->find();
    }



    /**
     * [delMenu 删除供租]
     * @return [type] [description]
     * @author 【朱沪宁】
     */
    public function delLease($id)
    {
        try{
            $this->where('id', $id)->delete();
            return ['code' => 1, 'data' => '', 'msg' => '供租删除成功'];
        }catch( PDOException $e){
            return ['code' => 0, 'data' => '', 'msg' => $e->getMessage()];
        }
    }

    /**
     * 根据搜索条件获取用户列表信息
     * @author 【朱沪宁】
     */
    public function getLeaseByWherePhoto($map, $Nowpage, $limits)
    {
        return  $this->field('think_lease.*,think_equipment.title as equipmentname,think_equipment.ggxh,think_equipment.sum,think_equipment.position,think_equipment.type,think_organization.title as orgname,think_equipment_cate.typephoto')
        ->join('think_equipment', 'think_equipment.id = think_lease.equipment_id')
        ->join('think_organization', 'think_organization.id = think_equipment.organization_id')
        ->join('think_equipment_cate', 'think_equipment.cate_id = think_equipment_cate.id')
        ->where($map)->page($Nowpage, $limits)->order('id desc')->select();
        //echo Db::name("think_lease")->getLastSql();
       // return null;
    }

}